﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security.Cryptography;
using DelonixRegiaHMSys.Core.Services;
using DelonixRegiaHMSys.Core.Models;
using DelonixRegiaHMSys.Infrastructure.Data;
using DelonixRegiaHMSys.UI.Models;

namespace DelonixRegiaHMSys.UI.WebForms
{
    public partial class AddSystemAccount : System.Web.UI.Page
    {
        string userID;
        protected void Page_Load(object sender, EventArgs e)
        {
            userID = Request.QueryString["UserID"];

        }

        protected void Addbtn_Click(object sender, EventArgs e)
        {
            var saservice = new saServices(new EfSystemAccountQuery());
            bool a = saservice.StaffHasAccount(DropDownListS.SelectedValue);
            if (a == true)
            {
                lblError.Text = "Staff already has an account!";
            }
            else
            {

                SystemAccount sa = new SystemAccount();
                sa.UserID = UserIDlbl.Text;
                sa.Password = Passwordtbx.Text;
                sa.AccessLevel = DropDownListAL.SelectedValue;
                sa.StaffID = DropDownListS.SelectedValue;
                sa.UserStatus = "OFF";


                saservice.AddUser(sa);

                Server.Transfer("~/WebForms/SystemAccountModule/SystemAccountManagement.aspx?UserID=" + userID);
            }
        }

        protected void DropDownListAL_SelectedIndexChanged(object sender, EventArgs e)
        {
            var stservice = new stServices(new EfStaffQuery());
            string dutiesName;
            if (DropDownListAL.SelectedValue == "FS")
            {
                dutiesName = "FS";
                DropDownListS.DataValueField = "staffID";
                DropDownListS.DataSource = stservice.RetrieveStaff(dutiesName).ToList();
                DropDownListS.DataBind();
            }
            else if (DropDownListAL.SelectedValue == "MS")
            {
                dutiesName = "MS";
                DropDownListS.DataValueField = "staffID";
                DropDownListS.DataSource = stservice.RetrieveStaff(dutiesName).ToList();
                DropDownListS.DataBind();
            }
            else
            {
                dutiesName = "HA";
                DropDownListS.DataValueField = "staffID";
                DropDownListS.DataSource = stservice.RetrieveStaff(dutiesName).ToList();
                DropDownListS.DataBind();
            }
        }
    }
}